# -*- coding: UTF-8 -*-
import pandas as pd
import re,sys,urllib.request,datetime
pd.set_option('display.expand_frame_repr', False)
print("程序开始运行。。。")
# 统计程序开始时间
starttime = datetime.datetime.now()
stock_code="sz000519"
# for i in range(100,800):
#     stock_code+=",sz000"+str(i)
# print(i,stock_code)
url='http://hq.sinajs.cn/list='+stock_code
print(url)
page = urllib.request.urlopen(url)
html = page.read()
html=html.decode("gb2312")
print(html)
data = re.compile(r'="(.*?)";')
datalist = re.findall(data,html)
all_item = datalist[0].split(',')
# stock_name_orig = all_item[0].decode("GB2312")
# stock_name_final = stock_name_orig.encode("UTF-8")
print(all_item)


dataxxx=[]
dataTitle=[]

for i in range(0,33):
    dataxxx.append(all_item[i]) #
    dataTitle.append("xxx["+str(i)+"]")

# print(dataTitle)
# print(dataxxx)
# xx=[['中兵红箭', '21.960', '22.520'],['中兵红箭', '21.960', '22.520']]
#
# for i in range(4):
#     xx.append(['中兵红箭', '21.960', '22.520'])
title=[
    "股票名称",
    "开盘价",
    "昨日收盘",
    "当前价格","最高价格","最低价格","买入价","卖出价","成交数量","成交金额",
    "买1量","买1价","买2量","买2价","买3量","买3价","买4量","买4价","买5量","买5价",
    "卖1量","卖1价","卖2量","卖2价","卖3量","卖3价","卖4量","卖4价","卖5量","卖5价",
    "日期","时间","状态"
    ]
df = pd.DataFrame([dataxxx],columns=[title])
# print(df.columns)
print("DF=========================================================")
print(df)
stock_dict = {}
stock_dict['日期'] = all_item[30]
stock_dict['时间'] = all_item[31]
stock_dict['状态'] = all_item[32]
stock_dict['股票名称'] = all_item[0]#stock_name_final
stock_dict['开盘价'] = all_item[1]
stock_dict['昨日收盘'] = all_item[2]
stock_dict['当前价格'] = all_item[3]
stock_dict['最高价格'] = all_item[4]
stock_dict['最低价格'] = all_item[5]
stock_dict['买入价'] = all_item[6]
stock_dict['卖出价'] = all_item[7]
stock_dict['成交数量'] = all_item[8]
stock_dict['成交金额'] = all_item[9]
stock_dict['买1量'] = all_item[10]
stock_dict['买1价'] = all_item[11]
stock_dict['买2量'] = all_item[12]
stock_dict['买2价'] = all_item[13]
stock_dict['买3量'] = all_item[14]
stock_dict['买3价'] = all_item[15]
stock_dict['买4量'] = all_item[16]
stock_dict['买4价'] = all_item[17]
stock_dict['买5量'] = all_item[18]
stock_dict['买5价'] = all_item[19]
stock_dict['卖1量'] = all_item[20]
stock_dict['卖1价'] = all_item[21]
stock_dict['卖2量'] = all_item[22]
stock_dict['卖2价'] = all_item[23]
stock_dict['卖3量'] = all_item[24]
stock_dict['卖3价'] = all_item[25]
stock_dict['卖4量'] = all_item[26]
stock_dict['卖4价'] = all_item[27]
stock_dict['卖5量'] = all_item[28]
stock_dict['卖5价'] = all_item[29]
stock_dict['type']="sina"
print("stock_dict = {}:\n",stock_dict)

data = [stock_dict]

dfx = pd.DataFrame(data)

print("df\n",dfx)
endtime = datetime.datetime.now()
print("\n程序运行时间：" + str(endtime - starttime))
print('=============================\t程序运行结束\t=============================')